Page({
  data: {
    addresses:[],
    is_default:"",
    choice:"",
    is_cheked:"",
    from_page:""
  },
  onLoad(query) {
    this.get_address();
    this.setData({
      from_page : query.from_page
    })
    // from_page = query.from_page;
  },

  topTap(e){
    console.log('in top');
    console.log(e);
  },

    editTap(e){
    console.log('你选择的框架是：', e)
    
    this.go_add_address(e.target.id);
  },

  go_add_address(edit_address_id){
    console.log('edit_address_id');
    
    console.log(edit_address_id.currentTarget);
    var url = `/pages/main/address-manage/address-manage?id=${edit_address_id}`;
    if(edit_address_id.currentTarget != undefined)
    url = `/pages/main/address-manage/address-manage?from_page=${this.data.from_page}`;
    my.navigateTo({
      url: url, // 需要跳转的 tabBar 页面的路径（需在 app.json 的 tabBar 字段定义的页面），路径后不能带参数
    });
  },

  checkTap(e){
    console.log('你选择的框架是：', e)
      
      var str = e.currentTarget.id;
     
      var pattern = "address";
      str = str.replace(new RegExp(pattern), "");
      console.log(str);
       my.redirectTo({
       url: `/pages/main/${this.data.from_page}/${this.data.from_page}?checkedAddressId=${str}`, // 需要跳转的应用内非 tabBar 的页面的路径，路径后可以带参数。参数与路径之间使用
     });

  },

  delTap(e){
    console.log('你选择的框架是：', e.target.id)
     var str = e.target.id;
    var pattern = "del";
      str = str.replace(new RegExp(pattern), "");
      console.log(str);

      var app = getApp()
      var url = app.globadData.apiUrlHeader+'del-address'
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
            "address_id":str,
            "access_token":app.globadData.access_token
        }),
        dataType:'json',
        success: (res) => {
          console.log(res.data);
        if(res.data.status==0)
        {
          this.get_address();
          my.showToast({
            content: '删除成功!', // 文字内容
            success: (res) => {
              
            },
          });
          
        }
      },
    });
  },

  radioChange: function(e) {
    console.log('你选择的框架是：', e.detail.value)

    //设置默认
    var app = getApp()
      var url = app.globadData.apiUrlHeader+'set-default-address'
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
            "address_id":e.detail.value,
            "access_token":app.globadData.access_token
        }),
        dataType:'json',
        success: (res) => {
          console.log(res.data);
        if(res.data.status==0)
        {
          // this.get_address();
          my.showToast({
            content: '设置成功!', // 文字内容
            success: (res) => {
              
            },
          });
          
        }
      },
    });

  },

  get_address(){
      var app = getApp()
      var url = app.globadData.apiUrlHeader+'get-address'
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
            "access_token":app.globadData.access_token
        }),
        dataType:'json',
        success: (res) => {
          console.log(res.data);
        if(res.data.status==0)
        {
          console.log(res.data.data);
          
          this.setData({
             addresses : res.data.data
          })
          
        }
      },
    });

  },
});
